<?php
class DigCms_Service_Plugin
{
    const DIGITALUS_KEY = 'digitalus_cms_plugins';
    /**
     * tests to determine if the plugins are registered
     * 
     */
    public static function areRegistered() {
        return Dig_Service_Core::hasParam(self::DIGITALUS_KEY);
    }
    /**
     * this method registers all of the plugins which are configured in the module config files
     * 
     * @example you configure the plugin as follows:
     * plugin.test.title = 'Test'
     * plugin.test.path = '/module/controller/action'
     * plugin.test.position = 3
     *
     * your plugin can optionally modify the current view's item (public only)
     * plugin.test.modify = "ServiceClass::methodToRun"
     *
     * you can optionally specify which items have this plugin enabled, in which case only these types of items will render the plugin
     * plugin.test.enabled.page = DigCms_Model_Page
     *
     * alternatively you can optionally specify which items have this plugin disabled, in which case everything that is not specified will use it
     * plugin.test.disabled.archive = DigCms_Model_Archive
     *
     */
    public static function registerPlugins() {
        foreach( Dig_Service_Module::listAll() as $module => $settings ) {
            if( isset($settings['plugin'])) {
                foreach ($settings['plugin'] as $plugin) {
                    self::registerPlugin($plugin);
                }
            }
        }
    }

    /**
     * registers a digitalus cms item form plugin, which renders in a new tab on the edit item form
     *
     * @param string $module
     * @param string $controller
     * @param string $action
     */
    public static function registerPlugin($config = array()) {
        $plugins = self::getPlugins();
        if(!$plugins) {
            $plugins = array();
        }
        $plugins[$config['position']] = $config;
        Dig_Service_Core::setParam(self::DIGITALUS_KEY, $plugins);
    }

    /**
     * returns an array of the currently registered plugins
     *
     * if you specify the optional type it will only return those plugins
     * 
     * @param string $type
     */
    public static function getPlugins($type = null) {
        if(Dig_Service_Core::hasParam(self::DIGITALUS_KEY)) {
            $plugins = Dig_Service_Core::getParam(self::DIGITALUS_KEY);
            if($type) {
                $allowed = array();
                foreach ($plugins as $position => $plugin) {
                    if(isset($plugin['enabled']) && is_array($plugin['enabled']) && in_array($type, $plugin['enabled'])) {
                        $allowed[$position] = $plugin;
                    }elseif(isset($plugin['disabled']) && is_array($plugin['disabled']) && ! in_array($type, $plugin['disabled'])) {
                        $allowed[$position] = $plugin;
                    }elseif(!isset($plugin['disabled']) && ! isset($plugin['enabled'])) {
                        $allowed[$position] = $plugin;
                    }
                }
            } else {
                $allowed = $plugins;
            }
            ksort($allowed);
            return $allowed;
        }
    }
}
